"""
公用办法
"""
import logging

from flask import session, g

from info.models import User
import functools


def do_index_class(index):
    """自定义过滤器，过滤点击排序html的class"""
    if index == 0:
        return "first"
    elif index == 1:
        return "second"
    elif index == 2:
        return "third"
    else:
        return ""


def login_user(view_fun):
    @functools.wraps(view_fun)  # 函数保持原来的名字
    def wrapper(*args, **kwargs):

        user_id = session.get("user_id", None)
        user = None
        if user_id:
            try:
                user = User.query.get(user_id)
            except Exception as e:
                user = None
                logging.error(e, "查询用户失败")
        # 使用g变量传值
        g.user = user
        return view_fun(*args, **kwargs)

    return wrapper
